import { signOut } from "../../api/auth";
import {
  fetchFishingSpotList,
  deleteFishingSpot,
} from "../../api/fishing-spots";

Page({
  data: {
    spotIndex: 0,
    spotName: "",
    spotId: null,
    fishingSpotList: [],
    spotSelectVisible: false,
  },
  onShow() {
    const app = getApp();
    console.log("----onShow mycenter------");

    fetchFishingSpotList().then((list) => {
      let spotIndex = 0;
      const spotId = app.globalData.spotId;
      // console.log("----onShow center------", list, spotId);
      if (spotId) {
        spotIndex = list.map((item) => item.id).indexOf(spotId);
        if (spotIndex === -1) {
          spotIndex = 0;
        }
      }
      const currentSpot = list[spotIndex];
      console.log("currentSpot", currentSpot, spotId, spotIndex);
      this.setData({
        fishingSpotList: list,
        spotIndex,
        spotId: currentSpot.id,
        spotName: currentSpot?.name,
      });
      app.changeSpotId(currentSpot.id);
    });
    this.getTabBar().init();
  },
  setCurrentSpot(index) {
    const { fishingSpotList } = this.data;
    const currentSpot = fishingSpotList[index];
    console.log("=====setCurrentSpot=====", currentSpot);
    this.setData({
      spotIndex: index,
      spotId: currentSpot.id,
      spotName: currentSpot?.name,
    });
    getApp().changeSpotId(currentSpot.id);
  },
  showSpotSelect() {
    this.setData({
      spotSelectVisible: true,
    });
  },
  closeSpotSelect() {
    this.setData({
      spotSelectVisible: false,
    });
  },
  onSpotSelectVisibleChange(e) {
    console.log("=====onSpotSelectVisibleChange", e.detail);
    if (e.detail.visible === false) {
      this.setData({
        spotSelectVisible: false,
      });
    }
  },
  onSpotSelected(e) {
    console.log("=====onSpotSelected", index);
    const { index } = e.currentTarget.dataset;
    this.setCurrentSpot(index);
    this.closeSpotSelect();
  },
  onSpotDelete(e) {
    const { index } = e.currentTarget.dataset;
    console.log("=====onSpotDelete", index);
    const { fishingSpotList } = this.data;
    const spotId = fishingSpotList[index]?.id;
    if (!spotId) {
      return;
    }
    wx.showModal({
      title: "删除钓场",
      content: "删除后数据将无法恢复，您确定要删除该钓钓场吗？",
      success: async (res) => {
        if (res.confirm) {
          const res = await deleteFishingSpot(spotId);
          if (res.error) {
            wx.showToast({
              title: "删除失败",
            });
            return;
          }
          fishingSpotList.splice(index, 1);
          this.setData({
            fishingSpotList,
            spotSelectVisible: false,
          });
          wx.showToast({
            title: "已删除",
            icon: "success",
          });
        }
      },
    });
  },
  onSpotAdd() {
    console.log("==onSpotAdd=");
    this.closeSpotSelect();
    wx.navigateTo({
      url: "/pages/fishingspot/setting/setting",
    });
    return;
  },
  onSpotUpdate() {
    let url = "/pages/fishingspot/setting/setting";
    const spotId = this.data.spotId;
    if (spotId) {
      url = `${url}?id=${spotId}`;
    }
    console.log("===", url);
    wx.navigateTo({
      url,
    });
  },
  onLogout() {
    wx.showModal({
      title: "退出登录",
      content: "您确定要退出登录吗？",
      success: async (res) => {
        if (res.confirm) {
          // 状态退出
          await signOut();
          wx.showToast({
            title: "已退出登录",
            icon: "success",
          });
        }
      },
    });
  },
});
